﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Globalization
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions

Namespace roncliProductions.LibWowAPI.PvP

    ''' <summary>
    ''' A class that retrieves arena team information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ArenaTeam
        Inherits WowAPIData

        Private atTeam As New Schema.arenateam

#Region "WowAPIData Overrides"

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri(String.Format(CultureInfo.InvariantCulture, "/api/wow/arena/{0}/{1}v{1}/{2}", Options.Realm, Options.TeamSize, Options.Name), UriKind.Relative)
            End Get
        End Property

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "LibWowAPI.ArenaTeam.{0}.{1}.{2}", Options.Realm, Options.TeamSize, Options.Name)
            End Get
        End Property

#End Region

#Region "Properties"

        ''' <summary>
        ''' The options for looking up arena team information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Options As New ArenaTeamOptions

        Private tTeam As Team
        ''' <summary>
        ''' Arena team information as returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Team As Team
            Get
                Return tTeam
            End Get
        End Property

#End Region

#Region "Methods"

#Region "Constructors"

        ''' <summary>
        ''' A default constructor to retrieve arena team information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve an arena team.
        ''' </summary>
        ''' <param name="strRealm"></param>
        ''' <param name="intTeamSize"></param>
        ''' <param name="strName"></param>
        ''' <remarks></remarks>
        Public Sub New(strRealm As String, intTeamSize As Integer, strName As String)
            Options.Realm = strRealm
            Options.TeamSize = intTeamSize
            Options.Name = strName
            Load()
        End Sub

#End Region

        ''' <summary>
        ''' Loads the arena team.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            atTeam = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.arenateam)), Schema.arenateam)
            tTeam = New Team(
                atTeam.realm,
                atTeam.ranking,
                atTeam.rating,
                atTeam.teamsize,
                Date.Parse(atTeam.created, CultureInfo.InvariantCulture),
                atTeam.name,
                atTeam.gamesPlayed,
                atTeam.gamesWon,
                atTeam.gamesLost,
                atTeam.sessionGamesPlayed,
                atTeam.sessionGamesWon,
                atTeam.sessionGamesLost,
                atTeam.lastSessionRanking,
                atTeam.side.GetSide(),
                atTeam.currentWeekRanking,
                (From m In atTeam.members
                 Select New Member(
                     New Character(
                         m.character.name,
                         m.character.realm,
                         m.character.class.GetClass(),
                         m.character.race.GetRace(),
                         CType(m.character.gender, Gender),
                         m.character.level,
                         m.character.achievementPoints,
                         m.character.thumbnail
                         ),
                     m.rank,
                     m.gamesPlayed,
                     m.gamesWon,
                     m.gamesLost,
                     m.sessionGamesPlayed,
                     m.sessionGamesWon,
                     m.sessionGamesLost,
                     m.personalRating
                     )
                 ).ToCollection()
             )
        End Sub

#End Region

    End Class

End Namespace
